package mystia.lorelei.app.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import mystia.lorelei.app.entity.User;
import mystia.lorelei.app.service.UserService;
import mystia.lorelei.app.util.Encrypt;
import mystia.lorelei.app.util.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result<Object> login(@RequestBody User user) {
        if (userService.login(user)) {
            return Result.ok("jwt");
        }
        return Result.fail();
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public Result<Object> register(@RequestBody User user) {
        if (userService.getUserByUsername(user.getUsername()) != null) {
            return Result.fail();
        }
        user.setPassword(Encrypt.sha512(user.getPassword()));
        userService.saveUser(user);
        return Result.ok();
    }
    
}
